package com.example.imchat.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.imchat.R;
import com.example.imchat.controller.activity.LoginActivity;
import com.example.imchat.model.Model;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;

public class SettingFragment extends Fragment {

    private Button bt_setting_out;
    private TextView tv_setting_name;
    private TextView tv_setting_id;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_setting,null);

        initView(view);
        return view;
    }

    private void initView(View view) {
        bt_setting_out = (Button)view.findViewById(R.id.bt_setting_out);
        tv_setting_name = (TextView)view.findViewById(R.id.tv_setting_name);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();

    }

    private void initData() {
        //在button上显示用户名称
        bt_setting_out.setText("退出登录");
        tv_setting_name.setText(EMClient.getInstance().getCurrentUser());
        // 退出登录的处理
        bt_setting_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        //退出环信服务器
                      EMClient.getInstance().logout(false, new EMCallBack() {
                          @Override
                          public void onSuccess() {
                              //账号退出时要关闭已经打开的数据表
                              Model.getInstance().getDbManager().close();

                              getActivity().runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                      //回到登录页面
                                      Toast.makeText(getActivity(),"退出成功",Toast.LENGTH_SHORT).show();

                                      //更新ui显示
                                      Intent intent = new Intent(getActivity(), LoginActivity.class);
                                      startActivity(intent);
                                      getActivity().finish();

                                  }
                              });


                          }

                          @Override
                          public void onError(int i, final String s) {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                   Toast.makeText(getActivity(),"退出失败"+s,Toast.LENGTH_SHORT).show();
                                }
                            });
                          }

                          @Override
                          public void onProgress(int i, String s) {

                          }
                      });
                    }
                });
            }
        });


    }
}
